/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.ac.aptech.fat1.c0910g.util;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletContext;

/**
 * @author NDC
 */
public class DBUtility {

    public static Connection getConnection(ServletContext context) throws Exception {
        try {
            String driver = context.getInitParameter("dbDriver");
            Class.forName(driver);
            String dbUrl = context.getInitParameter("dbUrl");
            String dbUser = context.getInitParameter("dbUser");
            String dbPassword = context.getInitParameter("dbPassword");
            return DriverManager.getConnection(dbUrl, dbUser, dbPassword);
        } catch (Exception e) {
            throw e;
        }
    }

    public static void closeConnection(Connection conn) throws Exception {
        if (conn != null && !conn.isClosed()) {
            try {
                conn.close();
            } catch (Exception ex) {
                throw ex;
            }
            conn = null;
        }
    }
}
